/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cs4280.dao.*;
import cs4280.bean.*;
import java.util.ArrayList;
import javax.servlet.http.HttpSession;
import java.util.Iterator;

/**
 *
 * @author PangPei
 */
public class requestControll extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void prepareRefund(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        ArrayList<Booking> bookings = new ArrayList<Booking>();

        Member member = (Member) session.getAttribute("member");
        if (member != null && member.getRole().equalsIgnoreCase("Customer")) {
            int memberid = member.getId();
            BookingDAO bookingDAO = new BookingDAO();
            bookings = bookingDAO.getRefundBookings(memberid);
        }
        this.setMovieNames(bookings);
        request.setAttribute("bookings", bookings);

    }

    public void doRefund(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        BookingDAO bookingDAO = new BookingDAO();

        int bookingid = Integer.parseInt(request.getParameter("bookingid"));
        switch (bookingDAO.changeBookingRequest(1, bookingid)) {
            case OK:
                request.setAttribute("msg2", "The request for refund has been submitted successfully.");
                break;
            case FAIL:
                request.setAttribute("msg2", "Something error happens.The request is failed to submit.");
                break;
        }

    }

    public void setMovieNames(ArrayList<Booking> bookings) {
        MovieDAO movieDAO = new MovieDAO();
        String moviename;
        Booking booking;
        Iterator<Booking> it = bookings.iterator();
        while (it.hasNext() != false) {
            booking = (Booking) it.next();
            moviename = movieDAO.getMoviename(booking.getMovieid());
            booking.setMoviename(moviename);
        }
    }

    public ArrayList<Booking> getRequestBookings() {
        ArrayList<Booking> bookings = new ArrayList<Booking>();

        BookingDAO bookingDAO = new BookingDAO();
        bookings = bookingDAO.getRequestBookings();
        this.setMovieNames(bookings);
        return bookings;
    }

    public void doAcceptRefund(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BookingDAO bookingDAO = new BookingDAO();

        int bookingid = Integer.parseInt(request.getParameter("bookingid"));
        Booking booking = bookingDAO.getBooking(bookingid);
        int memberid = Integer.parseInt(booking.getMemberid());
        int c_LP = booking.getPayment();
        //change loyalty point of the member after refund is made successfully
        loyaltyControll loyaltyC = new loyaltyControll();
        
        switch (bookingDAO.acceptRefundRequest(bookingid)) {
            case OK: {
                request.setAttribute("msg", "The request has been accepted successfully.");
                loyaltyC.minusLoyaltyPoints(memberid, c_LP);
                break;
            }
            case FAIL:
                request.setAttribute("msg", "Something error happens.Please try again.");
                break;
        }

    }

    public void doRejectRefund(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BookingDAO bookingDAO = new BookingDAO();

        int bookingid = Integer.parseInt(request.getParameter("bookingid"));
        switch (bookingDAO.rejectRefundRequest(bookingid)) {
            case OK:
                request.setAttribute("msg", "The request has been rejected successfully.");
                break;
            case FAIL:
                request.setAttribute("msg", "Something error happens.Please try again.");
                break;
        }

    }
}
